using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(C2DSprite))]
public class C2DSpriteEditor : Editor 
{
	public override void OnInspectorGUI()
	{
		C2DSprite sprite = (C2DSprite)(target);
		
		if( !sprite.IsInitialized() )
			sprite.InitSprite();
		
		DrawDefaultInspector();
		DrawSpriteInspector( sprite );
	}
	
	static public void DrawSpriteInspector( C2DSprite Sprite )
	{
		UFTAtlasMetadata Atlas = Sprite.GetAtlas();
		
		if( Atlas != null )
		{	
			//Generate a list of sprite
			
			string[] SpriteList = new string[Atlas.entries.Length];
			
			for( int nSprite = 0; nSprite < Atlas.entries.Length; nSprite++ )
			{
				SpriteList[nSprite] = Atlas.entries[nSprite].name;
			}
					
			int nIndex = EditorGUILayout.Popup( "CurrentSprite ", Sprite.GetSpiteIndex(), SpriteList, EditorStyles.popup);
	
			C2DSprite.AnchorPoint nAnchor =(C2DSprite.AnchorPoint)EditorGUILayout.EnumPopup( "Acnhor", Sprite.GetAnchor() );
			
			Atlas = (UFTAtlasMetadata)EditorGUILayout.ObjectField( "Atlas", Atlas, typeof( UFTAtlasMetadata ), false );
			
			if( GUI.changed )
			{	
				if( Atlas != Sprite.GetAtlas() )
				{
					Sprite.SetAtlas( Atlas );
				}
				else if( nIndex != Sprite.GetSpiteIndex() )
				{
					Sprite.SetSpriteIndex( nIndex );
				}
				else if( nAnchor != Sprite.GetAnchor() )
				{
					Sprite.SetAnchor( nAnchor );
				}
			}
		}
	}
}
